class Printer:
	
	BAR_HEIGHT = 0.5
	BAR_MAX_LENGTH = 8
	BARS_DIST = 1.5
	BAR_TITLE_X = -0.5
	
	def __init__(self):
		self.currentLinePos = 0
	
	def parseFile(self,filename):
		with open(filename, 'r') as f:
			for line in f:
				self.parseLine(line)
				
	def parseLine(self,line):
		splittedLine = line[:-1].split('|')
		label = splittedLine[0]
		yellowVal = float(splittedLine[1])
		blueVal = float(splittedLine[2])
		
		self.createBars(label, yellowVal, blueVal)
		
	def createBars(self,label, yVal, bVal):		
		base = self.currentLinePos
		self.printLabel(self.BAR_TITLE_X ,base + self.BAR_HEIGHT,label,'east')
		
		#blue bar
		x = 0
		y = base
		length = ( bVal / (yVal + bVal) ) * self.BAR_MAX_LENGTH
		self.printBar(x,y,length,'blue',bVal)
		
		#yellow bar
		x = 0
		y = base + self.BAR_HEIGHT
		length = ( yVal / (yVal + bVal) ) * self.BAR_MAX_LENGTH
		self.printBar(x,y,length,'yellow',yVal)
		
		#incrementing counters
		self.currentLinePos += self.BARS_DIST
		
		
	def printLabel(self, x, y, label, anchor):
		print "\\draw ({0:.2f},{1:.2f}) node[anchor={2}]{{{3}}};".format(x,y,anchor,label)
		 
	def printBar(self,x,y,length,color,label):
		#\draw[line width=1pt, color = black, fill = yellow] (0,0.5) -- (6,0.5) -- (6,1) -- (0,1) -- (0,0.5);
		#printing bar
		print "\\draw[line width=1pt, color = black, fill = {0}]" \
			"({1:.2f},{2:.2f})--({3:.2f},{4:.2f})--({5:.2f},{6:.2f})--({7:.2f},{8:.2f})--({1:.2f},{2:.2f});" \
			.format(color, x,y, x,y+self.BAR_HEIGHT, x+length,y+self.BAR_HEIGHT, x+length,y)
			
		#adding label TODO		
		self.printLabel(x + length, y + (self.BAR_HEIGHT / 2.0), label, 'west')
		

#-----------------------------------------------------------------------

def printHeader():
	print "\\begin{tikzpicture}"
	
def printFooter():
	print "\\end{tikzpicture}"

def main(filename):
	printHeader()
	p = Printer()
	p.parseFile(filename)
	printFooter()
	
#-----------------------------------------------------------------------	

if __name__ == "__main__":
	import sys
	if len(sys.argv) != 2 :
		exit()
	main(sys.argv[1])
